/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.action;

import cms.sessionbean.UserPortfoiloRemote;
import cms.util.Constants;
import cms.util.EjbLocator;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dto.User;

/**
 *
 * @author Gary
 */
public class AuthenticateAction extends ActionSupport{
    
    public AuthenticateAction() {
    }
    
    public String execute() throws Exception {
//        User user = new User("admin", "admin");
//        if(username.equalsIgnoreCase(user.getUserName()) && password.equalsIgnoreCase(user.getPassword())){
//            return  Constants.SUCCESS;
//        } else {
//          addFieldError("login", "Invalid User.");   
//        return "input";
//        }
        UserPortfoiloRemote adminSession =  (UserPortfoiloRemote) EjbLocator.getInstance().LookUpBeanByName(UserPortfoiloRemote.class, "UserPortfoilo");
        if (adminSession.userLogin(username, password, "admin"))
        {
            ActionContext.getContext().getSession().put("adminName" , username); 
            return Constants.SUCCESS; 
        }
        else 
        {
          addFieldError("login", "Invalid User.");   
            return "input";
        }
       
    }
    
    public void validate() {
	if (username.trim().length() == 0) {
            addFieldError("username", "User Name is rquired");
	}
        if (password.trim().length() == 0) {
            addFieldError("password", "Password is rquired");
	}
    }
        private String username;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	private String password;
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return password;
	}
}
